package com.ts.proj.controller;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.entity.*;
import com.ts.proj.service.CustomerService;
import com.ts.proj.service.MaterialService;
import com.ts.proj.service.SupplierService;
import com.ts.proj.service.SystemConstantService;
import com.ts.util.JsonUtils;
import com.ts.util.JurisdictionUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

//说明 字符串：Test，作为替换字符串

@Controller
public class MeasureController extends BaseController {

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private SystemConstantService systemConstantService;
    
    @RequestMapping("/business-operation-manage")
    public ModelAndView weighingOperation(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "business/measure/operation-manage";
            PageData pd = new PageData();
            pd.put("deleteFlag", "N");
            List<SupplierEntity> supplierEntities = supplierService.findAll(pd);
            List<CustomerEntity> customerEntities = customerService.findAll(pd);
            List<MaterialEntity> materialEntities = materialService.findAll(pd);
            SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            modelAndView.addObject("user",systemUserEntity);
            modelAndView.addObject("supplier",supplierEntities);
            modelAndView.addObject("customer",customerEntities);
            modelAndView.addObject("materials",materialEntities);
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping(value = "/business-operation-manage2")
    public ModelAndView businessOperationManage2(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "business/measure/operation-manage2";
            PageData pd = new PageData();
            pd.put("deleteFlag", "N");
            List<SupplierEntity> supplierEntities = supplierService.findAll(pd);
            List<CustomerEntity> customerEntities = customerService.findAll(pd);
            List<MaterialEntity> materialEntities = materialService.findAll(pd);
            SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            modelAndView.addObject("user",systemUserEntity);
            List<PageData> supplierList = new ArrayList<>();
            for(SupplierEntity supplierEntity : supplierEntities){
                PageData temp = new PageData();
                temp.put("value", supplierEntity.getSupplierId()+"|"+supplierEntity.getSupplierPY());
                temp.put("name", supplierEntity.getSupplierName());
                supplierList.add(temp);
            }
            List<PageData> customerList = new ArrayList<>();
            for(CustomerEntity customerEntity:customerEntities){
                PageData temp = new PageData();
                temp.put("value", customerEntity.getCustomerId()+"|"+customerEntity.getCustomerPY());
                temp.put("name", customerEntity.getCustomerName());
                customerList.add(temp);
            }
            List<PageData> materialList = new ArrayList<>();
            for(MaterialEntity materialEntity : materialEntities){
                PageData temp = new PageData();
                temp.put("value", materialEntity.getMaterialId()+"|"+ materialEntity.getMaterialPY());
                temp.put("name",materialEntity.getMaterialName());
                materialList.add(temp);
            }
            modelAndView.addObject("supplier",supplierList);
            modelAndView.addObject("customer",customerList);
            modelAndView.addObject("materials",materialList);
            String ip = this.getRequest().getRemoteAddr();
            modelAndView.addObject("ip", ip);
            PageData pdConstant = new PageData();
            pdConstant.put("constantKey", "ip-location");
            pdConstant.put("constantValue", ip);
            List<SystemConstantEntity> all = systemConstantService.findAll(pdConstant);
            if(all.size()==1){
                modelAndView.addObject("flag", all.get(0).getConstantValueDesc());
                    modelAndView.addObject("desc", all.get(0).getConstantValueDesc().equals("loc1")?"进厂方向计量点":"出厂方向计量点");
            }else{
                modelAndView.addObject("flag", "error");
                modelAndView.addObject("desc", "error");
            }

        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping(value = "/business-measure-add")
    public ModelAndView businessMeasureAdd(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "business/measure/measure-add";
            PageData pd = new PageData();
            pd.put("deleteFlag", "N");
            List<SupplierEntity> supplierEntities = supplierService.findAll(pd);
            List<CustomerEntity> customerEntities = customerService.findAll(pd);
            List<MaterialEntity> materialEntities = materialService.findAll(pd);
            SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            modelAndView.addObject("user",systemUserEntity);
            List<PageData> supplierList = new ArrayList<>();
            for(SupplierEntity supplierEntity : supplierEntities){
                PageData temp = new PageData();
                temp.put("value", supplierEntity.getSupplierId()+"|"+supplierEntity.getSupplierPY());
                temp.put("name", supplierEntity.getSupplierName());
                supplierList.add(temp);
            }
            List<PageData> customerList = new ArrayList<>();
            for(CustomerEntity customerEntity:customerEntities){
                PageData temp = new PageData();
                temp.put("value", customerEntity.getCustomerId()+"|"+customerEntity.getCustomerPY());
                temp.put("name", customerEntity.getCustomerName());
                customerList.add(temp);
            }
            List<PageData> materialList = new ArrayList<>();
            for(MaterialEntity materialEntity : materialEntities){
                PageData temp = new PageData();
                temp.put("value", materialEntity.getMaterialId()+"|"+ materialEntity.getMaterialPY());
                temp.put("name",materialEntity.getMaterialName());
                materialList.add(temp);
            }
            modelAndView.addObject("supplier",supplierList);
            modelAndView.addObject("customer",customerList);
            modelAndView.addObject("materials",materialList);

        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    
}